<?php
require('./login.php');

$data = json_decode(file_get_contents('php://input'), true);

if (empty($data["userName"])||empty($data["password"])) {
    die(json_encode(array("code" => 400, "msg" => "用户名或密码格式错误", "data" => null)));
}

$userName = $data["userName"];

$password = $data["password"];

$query = mysqli_query($connect, "select count(*) as count from users where username='{$userName}' and password='{$password}';");
if (!$query) {
    die(json_encode(array("code" => 500, "msg" => "服务器异常", "data" => null)));
};

$result = mysqli_fetch_assoc($query)["count"];

if ($result > 0) {
    session_id("admin");
    session_start();
    $_SESSION['admin'] = md5(md5($userName));
    die(json_encode(array("code" => 0, "msg" => "登录成功", "data" => $_SESSION['admin'])));
} else {
    die(json_encode(array("code" => 1010, "msg" => "用户名或密码错误", "data" => null)));
}
